package com.dtjd.playcoinmonkey.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.dtjd.playcoinmonkey.R;
import com.dtjd.playcoinmonkey.adapter.RecommendListAdapter;
import com.dtjd.playcoinmonkey.bean.HttpParaBean;
import com.dtjd.playcoinmonkey.bean.RecommendListBean;
import com.dtjd.playcoinmonkey.databinding.ActivityManageRecommendBinding;
import com.dtjd.playcoinmonkey.utils.Network;
import com.dtjd.playcoinmonkey.utils.Tools;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ManageRecommendActivity extends AppCompatActivity {
    private RecommendListAdapter adp;
    private int current=1;
    private final List<RecommendListBean> data=new ArrayList<>();
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.manage_recommend, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){ finish(); }
        if (item.getItemId()==R.id.menu_manage_recommend_add){
            startActivity(new Intent(ManageRecommendActivity.this,AddRecommendActivity.class));
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManageRecommendBinding binding = ActivityManageRecommendBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setTitle(getResources().getString(R.string.manageRecommend));
        ActionBar actionbar = getSupportActionBar();
        if (actionbar!=null){
            actionbar.setDisplayHomeAsUpEnabled(true);
            actionbar.setHomeButtonEnabled(true);
        }
        adp=new RecommendListAdapter(this,data);
        binding.manageRecommendList.setAdapter(adp);
        binding.manageRecommendRecommendRefresh.setRefreshHeader(new ClassicsHeader(this));
        binding.manageRecommendRecommendRefresh.setRefreshFooter(new ClassicsFooter(this));
        binding.manageRecommendRecommendRefresh.setOnRefreshListener(refreshLayout ->{
            current=1;
            refresh(refreshLayout,null);
        });
        binding.manageRecommendRecommendRefresh.setOnLoadMoreListener(refreshLayout -> refresh(null,refreshLayout));
        binding.manageRecommendList.setOnItemClickListener((adapterView, view, i, l) -> {
            RecommendListBean bean = data.get(i);
            Tools.showDialog(ManageRecommendActivity.this, getResources().getString(R.string.tips), getResources().getString(R.string.sureDelete), getResources().getString(R.string.confirm), (o, dialog) -> {
                dialog.dismiss();
                List<HttpParaBean> para=new ArrayList<>();
                para.add(new HttpParaBean("id",bean.id));
                para.add(new HttpParaBean("token","token"));
                Network.post(para, "admin/deleteRecommend", new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        e.printStackTrace();
                    }
                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) {
                        runOnUiThread(() -> {
                            try {
                                Tools.toast(new JSONObject(Objects.requireNonNull(response.body()).string()).getString("datalist"));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            refresh(null,null);
                        });
                    }
                });
            }, getResources().getString(R.string.cancel), (o, dialog) -> dialog.dismiss());

        });
        refresh(null,null);
    }
    private void refresh(RefreshLayout refreshLayout, RefreshLayout loadmorelayout) {
        if (loadmorelayout!=null&&refreshLayout==null) current++;
        if (refreshLayout!=null&&loadmorelayout==null) current=1;
        ArrayList<HttpParaBean> list=new ArrayList<>();
        list.add(new HttpParaBean("current",String.valueOf(current)));
        list.add(new HttpParaBean("size", "5"));
        Network.post(list, "wbhMain/recommendPosts", new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
               runOnUiThread(() -> {
                    try {
                        JSONObject jo1=new JSONObject(response.body().string());
                        JSONArray ja = jo1.getJSONArray("datalist");
                        if (loadmorelayout==null) data.clear();
                        if (ja.length()!=0){
                            for (int i = 0; i < ja.length(); i++){
                                JSONObject jo = ja.getJSONObject(i);
                                JSONArray jo_imgs = jo.getJSONArray("imgs");
                                String img_url="";
                                if (jo_imgs.length()>0) img_url= jo_imgs.getString(0);
                                data.add( new RecommendListBean(jo.getString("id"),jo.getInt("bp_click_number"), jo.getString("bp_content"), jo.getString("bp_title"), img_url, jo.getString("wb_nickname"), jo.getString("wb_url"), jo.getString("time")));
                            }
                        }
                        adp.notifyDataSetChanged();
                        if (refreshLayout!=null)refreshLayout.finishRefresh(0);
                        if (loadmorelayout!=null)loadmorelayout.finishLoadMore(0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            }
        });
    }

}